package game;

import game.exceptions.GameException;
import game.exceptions.GroupException;
import game.exceptions.SetException;
import java.util.Vector;

/**
 * Abstract class that have all common features for group and run in Rummikub game; 
 * @author Fishman Yevgeni
 * @date Oct 31, 2009
 *
 */
public abstract class Set{
	
	protected Vector<Tile> _tiles;
	
	protected Set(Vector<Tile> tiles) throws SetException{
		if (tiles.size() < 3) {
			throw new SetException("Attempt to create a set with less that 3 tiles.");
		}
		this._tiles = tiles;
	}
	
	public int getTilesCount(){
		return _tiles.size();
	}
	
	public Tile getTileAt(int index){
		return _tiles.elementAt(index);
	}
	
	public boolean contains(Tile tile){
		return _tiles.contains(tile);
	}
	
	/**
	 * Adds tile to set.
	 * @param tile - tile to add.
	 * @throws GameException if sets correctness is violated.
	 */
	public abstract void addTile(Tile tile) throws GameException;
	
	/**
	 * Removes existing tile from a group. Group correctness will be preserved.
	 * @param tile - a Tile to remove.
	 * @throws GroupException if the tile don't exists in the group or if the removal will violate the group correctness.
	 */
	public void removeTile(Tile tile) throws SetException{
		if (_tiles.size() <= 3) {
			throw new SetException("Trying to remove tile from group with 3 tiles.");
		}
		if (!_tiles.contains(tile)) {
			throw new SetException("Trying to remove tile from group where it doesn't exists.");
		}
		_tiles.remove(tile);
	}
	
	

}
